package dao.manager.impl;

import dao.BaseDao;
import dao.manager.Borrow_booksDao;
import entity.Books;
import entity.Borrow_books;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Borrow_booksDaoIml
 *
 * @author 谭文斌
 * @since 2023/9/4
 */
public class Borrow_booksDaoIml extends BaseDao implements Borrow_booksDao {
    @Override
    public List<Borrow_books> getList(String condition, String conditionValue, int pageIndex, int pageSize) {
        String sql = "select * from borrow_books ";
        if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")){
            sql += " where " + condition + " like concat('%',?,'%')";
        }
        sql += " limit ?,?";
        List<Borrow_books> list = new ArrayList<>();
        Borrow_books books=null;
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            int num = 0;
            if (condition != null && conditionValue != null){
                pstmt.setString(++num,conditionValue);
            }
            pstmt.setInt(++num,pageIndex);
            pstmt.setInt(++num,pageSize);
            rs = pstmt.executeQuery();
            while (rs.next()){
                books = new Borrow_books();
                books.setId(rs.getInt("id"));
                books.setCard_id(rs.getInt("card_id"));
                books.setBook_id(rs.getInt("book_id"));
                books.setBorrow_date(rs.getString("borrow_date"));
                books.setEnd_date(rs.getString("end_date"));
                books.setReturn_date(rs.getString("return_date"));
                books.setIllegal(rs.getString("illegal"));
                books.setManager_id(rs.getString("manager_id"));
                list.add(books);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return list;
    }

    @Override
    public int pageCount(String condition, String conditionValue) {
        String sql = "select COUNT(*) c from borrow_books";
        if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")){
            sql += " where " + condition + " like concat('%',?,'%')";
        }
        int total = 0;
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            if (condition != null && conditionValue != null){
                pstmt.setString(1,conditionValue);
            }
            rs = pstmt.executeQuery();
            if (rs.next()){
                total = rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return total;
    }
}
